﻿namespace Ex03.GarageLogic
{
    using System;
    using System.Collections.Generic;
    using System.Text;

    internal class ElectricEngine : Engine
    {
        #region Costs
        private const float k_MinMinutes = 0f; 
        #endregion

        #region Messages
        private const string k_MinutesExceedsMaximum = 
@"Vehicle cannot be recharged because the amount of minutes entered exceeds the maximum amount.
{0} Minutes left to fill.";

        private const string k_Hours = "{0} Hours";
        #endregion

        #region Data Members
        private readonly float r_MaximumBatteryTimeInHours;
        private float m_BatteryLeftInHours;
        #endregion

        #region Constructors
        public ElectricEngine(float i_MaximumBatteryTimeInHours)
            : base()
        {
            this.r_MaximumBatteryTimeInHours = i_MaximumBatteryTimeInHours;
        }
        #endregion

        #region Properties
        public float MaximumMinutesAllowed 
        {
            get { return (float)(this.r_MaximumBatteryTimeInHours * 60); }
        }

        public string MinutesLeftToRecharge 
        {
            get { return ((float)(this.r_MaximumBatteryTimeInHours - this.m_BatteryLeftInHours)).ToString(); }
        }
        #endregion

        #region Public Methods
        /// <summary>
        /// This is a public method to charge battery by minuts
        /// </summary>
        /// <param name="i_MinutsToCharge"></param>
        public void AddBatterySource(float i_MinutsToCharge)
        {
            float updatedBatteryTime = this.m_BatteryLeftInHours + (i_MinutsToCharge / 60);

            if (i_MinutsToCharge > k_MinMinutes && updatedBatteryTime <= this.r_MaximumBatteryTimeInHours)
            {
                this.m_BatteryLeftInHours = updatedBatteryTime;
            }
            else
            {
                throw new ValueOutOfRangeException(string.Format(k_MinutesExceedsMaximum, this.MinutesLeftToRecharge), k_MinMinutes, this.MaximumMinutesAllowed);
            }
        }

        public override void UpdateCurrentCapacityByPercentage(float i_Percentage)
        {
            this.m_BatteryLeftInHours = (this.r_MaximumBatteryTimeInHours * i_Percentage) / 100;
        }

        public override float CalculatePercentageByCurrentEnergy()
        {
            return (this.m_BatteryLeftInHours / this.r_MaximumBatteryTimeInHours) * 100;
        }

        public override void GetAllDetails(ref List<string> io_ListOfVehicleDetails)
        {
            io_ListOfVehicleDetails.Add(GarageLogic.s_EmptyLine);
            io_ListOfVehicleDetails.Add(k_DisplayEngine);
            io_ListOfVehicleDetails.Add(string.Format(GarageLogic.s_PrintParameter, k_EngineType, Enum.GetName(typeof(eEngineType), 2)));
            io_ListOfVehicleDetails.Add(string.Format(GarageLogic.s_PrintParameter, k_CurrentEnergyCapacity, string.Format(k_Hours, m_BatteryLeftInHours.ToString())));
            io_ListOfVehicleDetails.Add(string.Format(GarageLogic.s_PrintParameter, k_MaximumEnergyCapacity, string.Format(k_Hours, r_MaximumBatteryTimeInHours.ToString())));
        }
        #endregion
    }
}
